%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% none
%
%==========================================================================

%==========================================================================
% The function "getRelevantSystemInfo" gets information about the system
% running it (such as operating system, processors, memory. etc.).
% INPUT: "lookForStrCellArr" is an optional cell vector whose cells specify
% strings of interest. Only the system information lines containing those
% strings and the lines accompanying them are returned. By default, the
% complete system information is returned.
% OUTPUT: "systemInfoLineCellArr" returns a cell array of strings, each
% cell holding one line of the relevant system information.
% EXAMPLE: 
% systemInfoLineCellArr = getRelevantSystemInfo({'OS', 'System Model', ...
%     'System Type', 'Processor', 'BIOS', 'Memory'});
%==========================================================================
function systemInfoLineCellArr = getRelevantSystemInfo(lookForStrCellArr)
    
% Get the system information in case of a Windows machine.
if (ispc == true)
    
    % Get the complete system information with a command line call.
    [status, systemInfoStr] = system('systeminfo');
    % If the system command failed in any way return a string describing
    % the reasons.
    if (status ~= 0)
        systemInfoLineCellArr = {systemInfoStr};
        return;
    end
    % Split the string holding the complete system information into lines.
    systemInfoLineCellArr = regexp(systemInfoStr, '\n', 'split');
    
    % If no strings of interest are specified, then return the complete
    % system information.
    if (nargin < 1)
        return;
    end
    
    % Find the lines that do not start with a white space.
    funHandle = @(x) ~(isempty(x)||isspace(x(1)));
    noInitWhiteSpaceIndArr = cellfun(funHandle, systemInfoLineCellArr);
    mainSystemInfoLineCellArr = systemInfoLineCellArr(noInitWhiteSpaceIndArr);
    
    % Allocate a boolean array to store flags indicating whether or not a
    % particular, not-white-space-starting line of the complete system
    % information is of interest.
    isStrFoundIndArr = false(size(mainSystemInfoLineCellArr));
    % Among the not-white-space-starting lines, find all lines holding any
    % of the specified strings of interest.
    for k = 1:numel(lookForStrCellArr)
        isStrFoundIndArr = isStrFoundIndArr | ...
            cellfun(@(x) (~isempty(x)), strfind(mainSystemInfoLineCellArr, lookForStrCellArr{k}));
    end
    
    % Find the indices of all lines of interest in the list of
    % not-white-space-starting lines.
    strFoundIndArr = find(isStrFoundIndArr);
    
    % Find the indices of the not-white-space-starting lines.
    noInitWhiteSpaceArr = find(noInitWhiteSpaceIndArr);
    
    % Allocate a boolean array to store flags indicating whether or not a
    % particular line of the complete system information is of interest.
    isOfInterestIndArr = false(size(systemInfoLineCellArr));
    
    % For each line found to be of interest make of interest the whole
    % stretch of white-space-starting lines that follow it immediately.
    for k = 1:numel(strFoundIndArr)
        isOfInterestIndArr(noInitWhiteSpaceArr(strFoundIndArr(k)): ...
            (noInitWhiteSpaceArr(min(strFoundIndArr(k) + 1, end)) - 1)) = true;
    end
    
    % Reduce the complete system information to the lines of interest only.
    systemInfoLineCellArr = systemInfoLineCellArr(isOfInterestIndArr);
else
    systemInfoLineCellArr = {};
end

return;
% end of the function "getRelevantSystemInfo"
